<?php defined('BASEPATH') OR exit('No direct script access allowed');
/**
 * Description: 收藏
 *
 * @author      Eden
 * @datetime    2016/12/18 22:20
 * @copyright   Beijing CmsTop Technology Co.,Ltd.
 */

class Collectlist_Model extends API_Model
{

    const PAGE_SIZE = 20;
    private $width = 320;
    private $height = 180;

    /**
     * 初始化
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * 主程序
     * @param array $params
     */
    public function run($params = array())
    {
        $send['state'] = false;
        do {

            if (!$this->checkParams($params)) {
                $send['error'] = $this->getError();
                break;
            }

            $page = empty($params['page']) ? 1 : intval($params['page']);
            $offset = ($page - 1) * self::PAGE_SIZE;

            if ($params['type'] == 'android') {
                $this->width = $this->width / 2;
                $this->height = $this->height / 2;
            }

            $rows = $this->getGoods($params, $offset, self::PAGE_SIZE);
            $send['state'] = true;
            if (!$rows) {
                $send['data'] = array(
                    'total' => 0,
                    'nextPage' => false,
                    'data' => []
                );
            } else {
                $send['data'] = array(
                    'total' => intval($rows['total']),
                    'nextPage' => ($offset + self::PAGE_SIZE) < $rows['total'],
                    'data' => $rows['data']
                );
            }
        } while(false);
        $this->output->send($send);
    }

    /**
     * 获取商品
     * @param array $params
     * @param $offset
     * @param int $limit
     * @return bool
     */
    private function getGoods(Array $params, $offset, $limit = 20)
    {
        $where = array('userId' => $params['userId']);
        try {
            $db = clone($this->db);
            $db->where($where);
            $total = $db->count_all_results('user_collect');
            if ($total == 0) {
                return false;
            }

            $q = $this->db->select('g.id as goodsId, g.goodsName, g.price, g.thumb, g.liveUrl, g.stars, g.streamId, g.expressWeight, g.status')
                ->from('base_goods as g')
                ->join('user_collect as uc', 'uc.goodsId = g.id')
                ->where($where)
                ->limit($limit, $offset)
                ->order_by('uc.created', 'DESC')
                ->get();
            $rows = $q->result_array();
            if (empty($rows)) {
                return false;
            }

            foreach($rows as &$item) {
                $item['goodsId'] = intval($item['goodsId']);
                $item['price'] = doubleval($item['price']);
                $item['thumb'] = thumb($item['thumb'], 200, 200);
                $item['isLive'] = !empty($item['streamId']);
                $item['liveUrl'] = value($item, 'liveUrl', '');
                unset($item['streamId']);
                $item['expressWeight'] = doubleval($item['expressWeight']);
                $item['status'] = $item['status'] == 1 ? 'onSale' : 'offSale';
                $item['stars'] = intval($item['stars']);
            }

            return array(
                'total' => $total,
                'data' => $rows
            );
        } catch(PDOException $e) {
            $this->setError('获取失败');
        }
        return false;
    }

    /**
     * 检查参数
     * @param $params
     * @return bool
     */
    private function checkParams($params)
    {
        if (empty($params['userId'])) {
            $this->setError('参数错误');
            return false;
        }

        return true;
    }

}
